﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using MySql.Data.MySqlClient;
using VirtualShare.Common;
using System.Windows.Forms;

namespace VirtualShare.Server
{
    class Program
    {
        static Socket server;
        public static List<Client> clients;
        public static MySqlConnection mysql;

        static void Main(string[] args)
        {
            clients = new List<Client>();

            Log.Write(ConsoleColor.Gray, "Connection au serveur MySql ... ");
            try
            {
                mysql = new MySqlConnection(String.Format("datasource=127.0.0.1;username={0};password={1};database=virtualshare", Config.MySql.username, Config.MySql.password));
                mysql.Open();
                Log.WriteLine(ConsoleColor.DarkGreen, "OK");
            }
            catch (Exception e)
            {
                Log.WriteLine(ConsoleColor.DarkRed, "Echec");
                MessageBox.Show(e.Message, "Erreur");
                Environment.Exit(-1);
            }

            Log.Write(ConsoleColor.Gray, "Ecoute du port 2424 ... ");
            try
            {
                server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Bind(new IPEndPoint(IPAddress.Any, 2424));
                server.Listen(10);
                Log.WriteLine(ConsoleColor.DarkGreen, "OK\n");
            }
            catch (Exception e)
            {
                Log.WriteLine(ConsoleColor.DarkRed, "Echec");
                MessageBox.Show(e.Message, "Erreur");
                Environment.Exit(-1);
            }

            while (true)
                clients.Add(new Client(server.Accept()));
        }

        
    }
}
